<?php
require_once(APP_OBJ_PATH . 'model.class.php');

/**
 * Nieuwsitem
 *
 * @package scorpion-magenta
 * @author Nicky van Meurs
 * @copyright 2012
 * @version 1.0
 * @access public
 */
class Nieuwsitem extends Model
{
	protected $NieuwsitemID;
	protected $Datum;
	protected $Titel;
	protected $Bericht;
	protected $AuteurID;
	protected $OntvangerID;

	public static function getAll()
	{
		$sql = "SELECT NieuwsitemID, Datum, Titel, Bericht, AuteurID, OntvangerID, UNIX_TIMESTAMP(Datum) as unixtimestamp FROM nieuwsitem  Order by unixtimestamp desc";
		global $db;
		$returnData = array ();
		foreach ($db->query($sql, PDO::FETCH_ASSOC) as $row)
			$returnData[] = new static($row);
		return $returnData;
	}

	public static function getItemsInArray()
	{
		$nieuwsitems = Nieuwsitem::getAll();
		$return = array();

		foreach($nieuwsitems as $index => $nieuwsitem)
		{
			if (($_SESSION['GroepID'] == 1 && $nieuwsitem->getProperty('AuteurID') == $_SESSION['Aanmaker'] ||
					($nieuwsitem->getProperty('OntvangerID') == $_SESSION['GebruikerID'])))
			{
				$return[] = array(
					'NieuwsitemID' => $nieuwsitem->getProperty('NieuwsitemID'),
					'Datum' => $nieuwsitem->getProperty('Datum'),
					'Titel' => $nieuwsitem->getProperty('Titel'),
					'Bericht' => $nieuwsitem->getProperty('Bericht'),
					'AuteurID' => $nieuwsitem->getProperty('AuteurID'),
					'NieuwsitemID' => $nieuwsitem->getProperty('OntvangerID'),
					);
			}
		}
		return $return;
	}
	public static function getPages($perPage, $total)
	{
		$pages = (intval($total)/intval($perPage));
		$round = ceil($pages);

		return intval($round);
	}

	public static function getStart($totalItems, $perPage, $page)
	{
		$totalPages = Nieuwsitem::getPages($perPage, $totalItems);



		unset($start);

		for($i = 0; $i <= $totalItems; $i++)
		{
			if(!isset($start))
			{
				if((floor(intval($i) / intval($perPage)) == ($page - 1)) && ((intval($i) / intval($perPage)) != ($page - 1)))
				{
					$start = $i;
				}
			}

		}
		if(isset($start))
				return $start;
	}
}
?>
